﻿using Newtonsoft.Json;
using Oplug.CoolTower.DTO;
using Oplug.CoolTower.Filters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower
{
    public partial class FormCustomization : Form
    {
        private SysConfig cfg;
        private Utilities Utility;
        private DataGridView DGV { get; set; }
        private string objName { get; set; }
        public FormCustomization(DataGridView dgv, string formName)
        {
            InitializeComponent();
            DGV = dgv;
            objName = formName + "_" + DGV.Name;
            CurrFormName.Text = formName;
        }
        [Description("个性化窗体加载")]
        private void FormCustomization_Load(object sender, EventArgs e)
        {
            Utility = new Utilities();
            cfg = Utility.GetSysCfg();
            string jsons = Utility.TxtRead(Utilities.LocalCfg);
            List<LocalConfig> datas = new List<LocalConfig>();
            if (!string.IsNullOrEmpty(jsons))
            {
                List<LocalConfig> lists = JsonConvert.DeserializeObject<List<LocalConfig>>(jsons);
                var items = lists.Where(o => o.ObjName == objName).ToList();
                for (int i = 0; i < DGV.Columns.Count; i++)
                {
                    LocalConfig obj = items.Where(o => o.FieldName == DGV.Columns[i].Name).FirstOrDefault();
                    if (obj != null)
                    {
                        obj.FieldDisplayIndex = obj.FieldDisplayIndex == 0 ? obj.FieldIndex : obj.FieldDisplayIndex;
                        datas.Add(obj);
                    }
                    else
                    {
                        obj = new LocalConfig();
                        obj.FieldIndex = i;
                        obj.FieldName = DGV.Columns[i].Name;
                        obj.FieldDisplay = DGV.Columns[i].Name;
                        obj.FieldWidth = 80;
                        obj.ObjName = objName;
                        obj.FieldDisplayIndex = i;
                        obj.IsReadOnly = true;
                        datas.Add(obj);
                    }
                }
            }
            if (datas == null || datas.Count <= 0)
            {
                for (int i = 0; i < DGV.Columns.Count; i++)
                {
                    LocalConfig obj = new LocalConfig();
                    obj.FieldIndex = i;
                    obj.FieldName = DGV.Columns[i].Name;
                    obj.FieldDisplay = DGV.Columns[i].Name;
                    obj.FieldWidth = 80;
                    obj.ObjName = objName;
                    obj.FieldDisplayIndex = i;
                    obj.IsReadOnly = true;
                    datas.Add(obj);
                }
            }
            dgvM.DataSource = datas;

            string[] captionArr = ("ObjName,编号,列名,显示名,列宽,隐藏,冻结,只读,列排序,格式").Split(',');
            for (int i = 0; i < captionArr.Length; i++)
            {
                if (i == 0)
                    dgvM.Columns[i].Visible = false;
                dgvM.Columns[i].HeaderText = captionArr[i];
            }
        }
        [Description("保存个性化方法")]
        private void toolSave_Click(object sender, EventArgs e)
        {
            if (!MethodFilters.IsDeny(MethodBase.GetCurrentMethod().Name, Name, cfg.DBConn))
            {
                dgvM.EndEdit();
                Utilities comm = new Utilities();
                string localcfg = comm.TxtRead(Utilities.LocalCfg);
                List<LocalConfig> listcfg = JsonConvert.DeserializeObject<List<LocalConfig>>(localcfg);
                if (listcfg != null)
                    listcfg = listcfg.Where(o => o.ObjName != objName).ToList();

                var currCfg = dgvM.DataSource as List<LocalConfig>;

                List<LocalConfig> datas = new List<LocalConfig>();
                if (listcfg != null)
                    datas = listcfg.Union(currCfg).ToList();
                else
                    datas = currCfg;

                string jsons = JsonConvert.SerializeObject(datas);
                comm.TxtWrite(Utilities.LocalCfg, jsons);
                Utilities.Message("设置成功！", InfoType.Information);
            }
        }

        private void ToolSort_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach(DataGridViewRow row in dgvM.Rows)
            {
                row.Cells["FieldIndex"].Value = i;
                row.Cells["FieldDisplayIndex"].Value = i;
                i++;
            }
        }
    }
}
